package com.faceCaptor.main;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.faceCaptor.entity.User;
import com.faceCaptor.util.HttpUtil;
import com.faceCaptor.util.JsonUtil;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SearchDialog extends Dialog {

	protected Object result;
	private Combo type;
	private Tree tree;
	private Map<String, String> creatorMap;


	/**
	 * Create the dialog.
	 * @param parent
	 */

	public SearchDialog(Shell parent) {
		super(parent);
		Map<String, String> employee = (Map<String, String>) MainView.cachedmap.get("employee");
		creatorMap = new HashMap<>();
		for (Map.Entry<String, String> entry : employee.entrySet()) {
			creatorMap.put(entry.getValue(), entry.getKey());
		}
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */

	/**
	 * Create contents of the dialog.
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		GridData data = new GridData(SWT.DEFAULT, 600);

		// data.
		container.setLayoutData(data);
		GridLayout gl_container = new GridLayout(5, true);
		gl_container.marginLeft = 20;
		gl_container.marginRight = 20;
		gl_container.marginTop = 20;
		container.setLayout(gl_container);

		CLabel label = new CLabel(container, SWT.NONE);
		data = new GridData();
		data.horizontalAlignment = SWT.CENTER;
		label.setText("查询类型:");
		label.setLayoutData(data);

		data = new GridData(GridData.FILL_HORIZONTAL);
		type=new Combo(container,SWT.DROP_DOWN | SWT.READ_ONLY);
		type.add("按客户类型查询");
		type.add("按店员查询");
		type.setLayoutData(data);

		Button button = new Button(container, SWT.NONE);
		data = new GridData();
		data.horizontalAlignment = GridData.FILL;
		button.setLayoutData(data);

		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				search();
			}
		});
		button.setText("查询");
		GridData gd_list = new GridData(SWT.LEFT, SWT.CENTER, false, false, 5, 5);
		gd_list.horizontalAlignment = SWT.FILL;
		gd_list.verticalAlignment = SWT.FILL;
		gd_list.grabExcessVerticalSpace = true;

		tree = new Tree(container, SWT.BORDER | SWT.H_SCROLL|SWT.V_SCROLL);
		tree.setHeaderVisible(true);
		tree.setLinesVisible(true);
		tree.setLayoutData(gd_list);


		//加入三列
		TreeColumn col1 = new TreeColumn(tree, SWT.NONE);
		col1.setText("客户姓名");
		col1.setWidth(100);

		TreeColumn col2 = new TreeColumn(tree, SWT.NONE);
		col2.setText("客户类型");
		col2.setWidth(80);

		TreeColumn col3 = new TreeColumn(tree, SWT.NONE);
		col3.setText("店员");
		col3.setWidth(100);

		return container;

	}
	
	@Override
    protected Point getInitialLocation(Point initialSize){
    	Point location = new Point(this.getParentShell().getLocation().x  
                + 150, this.getParentShell().getLocation().y  
                + 140); 
    	return location;  


    }
	
	@Override
    protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.NO_ID, "关闭", false);
    }

	private static void createItem(TreeItem parent, String s1, String s2, String s3) {
		TreeItem child1 = new TreeItem(parent, SWT.NONE);
		child1.setText(new String[] {s1, s2, s3});
	}

    public void search() {
		tree.removeAll();
		String params = "?shopId=" + MainView.cachedmap.get("shopId");
		String text = type.getText();
		if (text != null && !text.equals("")) {
			if (text.equals("按客户类型查询")) {
				params += "&type=0";
			} else {
				params += "&type=1";
			}
		} else {
			return;
		}
		String reqUrl = HttpUtil.HOST + HttpUtil.GROUP + params;
		System.out.println(reqUrl);
		String result = HttpUtil.sendGetRequest(reqUrl);
		if (result != null) {
			JSONObject jsonObject = JsonUtil.toJsonObject(result, "data");
			Map<String, List<User>> map = jsonObject.toJavaObject(new TypeReference<Map<String, List<User>>>(){});
			for (Map.Entry<String, List<User>> entry : map.entrySet()) {
				TreeItem item = new TreeItem(tree, SWT.NONE);
				if (text.equals("按客户类型查询")) {
					item.setText(entry.getKey());
				} else {
					item.setText(creatorMap.get(entry.getKey()));
				}
				List<User> userList = entry.getValue();
				for (User user : userList) {
					createItem(item, user.getUsername(), user.getType(), creatorMap.get(user.getCreator()));
				}
			}
		}
	}


	
}
